package main

import (
	"github.com/tealeg/xlsx"
	"strconv"
)

func printingparameter(equ []ModeFragment){
	var TempJudgeId []string
	var TempJudgeStartTime []string
	var TempJudgeEndTime []string
	var NewTempScore []int
	var OldTempScore []int
	var TempPattern []int
	var WaterPattern []int
	var day1 []Original
	for i := 0; i < len(equ); i++ {
		if equ[i].TempScoreJudgeFlag == true {
			TempJudgeId = append(TempJudgeId, equ[i].DevId)
			TempJudgeStartTime = append(TempJudgeStartTime, equ[i].StartTime)
			TempJudgeEndTime = append(TempJudgeEndTime, equ[i].EndTime)
			NewTempScore = append(NewTempScore, equ[i].NewTempScore)
			OldTempScore = append(OldTempScore, equ[i].TempScore)
			TempPattern = append(TempPattern,equ[i].TempPattern)
			WaterPattern = append(WaterPattern,equ[i].WaterPattern)
		}
	}

	for j,b,v,c,d,o,m,e:= 0,0,0,0,0,0,0,0; j < len(TempJudgeId) && b < len(TempJudgeStartTime) && v < len(TempJudgeEndTime) && c < len(NewTempScore) && d < len(OldTempScore) && m < len(TempPattern) && e < len(WaterPattern); j++ {
		DevId:= TempJudgeId[j]
		taskStartTime := TempJudgeStartTime[b]
		taskEndTime := TempJudgeEndTime[v]
		NewTempScore:= NewTempScore[c]
		OldTempScore:= OldTempScore[d]
		TempPattern := TempPattern[m]
		WaterPattern := WaterPattern[e]
		ScanningDB.Debug().Table("data1101_20210101").Where("datatime >= ? AND datatime< ? And applianceid=? ", taskStartTime, taskEndTime, DevId).Find(&day1)
		v++
		b++
		c++
		d++
		m++
		e++
		if  len(day1) == 0{
        break
		}else {
			for k, l := 0, len(day1); k < len(day1); k++ {
				file, err := xlsx.OpenFile("tempScore.xlsx")
				if err != nil {
					panic(err)
				}
				first := file.Sheets[o]
				row := first.AddRow()
				row.SetHeightCM(1)
				cell := row.AddCell()
				cell.Value = day1[k].CityCode
				cell = row.AddCell()
				cell.Value = strconv.Itoa(k)
				cell = row.AddCell()
				cell.Value = day1[k].Applianceid
				cell = row.AddCell()
				cell.Value = strconv.Itoa(day1[k].Flow)
				cell = row.AddCell()
				cell.Value = strconv.Itoa(day1[k].Outtemp)
				cell = row.AddCell()
				cell.Value = strconv.Itoa(day1[k].Settemp)
				cell = row.AddCell()
				cell.Value = strconv.Itoa(NewTempScore)
				cell = row.AddCell()
				cell.Value = strconv.Itoa(OldTempScore)
				cell = row.AddCell()
				cell.Value = strconv.Itoa(TempPattern)
				cell = row.AddCell()
				cell.Value = strconv.Itoa(WaterPattern)
				err = file.Save("tempScore.xlsx")
				if err != nil {
					panic(err)
				}
				l--
				if l == 0 {
					sheetName := "sheet"+ strconv.Itoa(j+1)
					var err error
					var sheet *xlsx.Sheet
					sheet,err = file.AddSheet(sheetName)
					if err != nil {
						panic(err)
					}
					row = sheet.AddRow()
					row.SetHeightCM(1) //设置每行的高度
					cell = row.AddCell()
					cell.Value = "CityCode"
					cell = row.AddCell()
					cell.Value = "DataTime"
					cell = row.AddCell()
					cell.Value = "Applianceid"
					cell = row.AddCell()
					cell.Value = "Flow"
					cell = row.AddCell()
					cell.Value = "Outtemp"
					cell = row.AddCell()
					cell.Value = "Settemp"
					cell = row.AddCell()
					cell.Value = "NewTempScore"
					cell = row.AddCell()
					cell.Value = "TempScore"
					cell = row.AddCell()
					cell.Value = "TempPattern"
					cell = row.AddCell()
					cell.Value = "WaterPattern"

					err1 := file.Save("tempScore.xlsx")
					if err1 != nil {
						panic(err)
					}
					o++
				}
			}
		}
		}
	}



	/*for j,b,v := 0,0,0; j < len(TempJudgeId) && b < len(TempJudgeStartTime) && v < len(TempJudgeEndTime); j++ {
		OriginalDB.AutoMigrate(&Original{})
		DevId:= TempJudgeId[j]
		taskStartTime := TempJudgeStartTime[b]
		taskEndTime := TempJudgeEndTime[v]
		ScanningDB.Table("data1101_20210101").Where("datatime >= ? AND datatime< ? And applianceid=? ", taskStartTime, taskEndTime, DevId).Find(&day1)
		v++
        b++
		for k := 0; k < len(day1); k++ {
			OriginalDB.Table("Originals").Create(&day1[k])
		}

	}
}*/
/*func PrintingparameterDeviceWrite(day1 []Original) []Original{
	monitor := make([]Original, len(day1), len(day1))
	for i := 0; i < len(day1); i++ {
		monitor[i].Applianceid = day1[i].Applianceid
		monitor[i].CityCode = day1[i].CityCode
		monitor[i].DataTime = day1[i].DataTime
		monitor[i].Flow = day1[i].Flow
		monitor[i].Outtemp = day1[i].Outtemp
		monitor[i].Settemp = day1[i].Settemp
		monitor[i].Flame = day1[i].Flame
	}
	return monitor
}*/
